/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.common.query.expression;

import java.util.Arrays;
import java.util.Collection;

import com.eelpo.common.query.criteria.Criteria;
import com.eelpo.common.util.BlankUtils;
import com.eelpo.common.util.StringUtils;

/**
 * <pre>
 * 
 * In表达式
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class InExpression implements ParameterCriterion {

	private boolean useAlias;
	private String operator;
	private String propertyName;
	private Collection<?> values;
	private Criteria criteria;

	private InExpression(boolean useAlias, String operator, String propertyName, Collection<?> values) {
		this.useAlias = useAlias;
		this.values = values;
		this.operator = operator;
		this.propertyName = propertyName;
	}

	private InExpression(boolean useAlias, String operator, String propertyName, Criteria criteria) {
		this.useAlias = useAlias;
		this.criteria = criteria;
		this.operator = operator;
		this.propertyName = propertyName;
	}

	public static InExpression getInExpression(String propertyName, Collection<?> values) {
		return new InExpression(true, IN, propertyName, values);
	}

	public static InExpression getInExpression(String propertyName, Object[] values) {
		return new InExpression(true, IN, propertyName, Arrays.asList(values));
	}

	public static InExpression getInExpression(String propertyName, Criteria criteria) {
		return new InExpression(true, IN, propertyName, criteria);
	}

	public static InExpression getNotInExpression(String propertyName, Collection<?> values) {
		return new InExpression(true, NOTIN, propertyName, values);
	}

	public static InExpression getNotInExpression(String propertyName, Object[] values) {
		return new InExpression(true, NOTIN, propertyName, Arrays.asList(values));
	}

	public static InExpression getNotInExpression(String propertyName, Criteria criteria) {
		return new InExpression(true, NOTIN, propertyName, criteria);
	}

	public static InExpression getInInornateExpression(String propertyName, Collection<?> values) {
		return new InExpression(false, IN, propertyName, values);
	}

	public static InExpression getInInornateExpression(String propertyName, Object[] values) {
		return new InExpression(false, IN, propertyName, Arrays.asList(values));
	}

	public static InExpression getInInornateExpression(String propertyName, Criteria criteria) {
		return new InExpression(false, IN, propertyName, criteria);
	}

	public static InExpression getNotInInornateExpression(String propertyName, Collection<?> values) {
		return new InExpression(false, NOTIN, propertyName, values);
	}

	public static InExpression getNotInInornateExpression(String propertyName, Object[] values) {
		return new InExpression(false, NOTIN, propertyName, Arrays.asList(values));
	}

	public static InExpression getNotInInornateExpression(String propertyName, Criteria criteria) {
		return new InExpression(false, NOTIN, propertyName, criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getJpql(java.lang.
	 *      StringBuilder)
	 */
	@Override
	public StringBuilder getJpql(StringBuilder jpql, String ailas) {
		jpql.append(AND);
		if (useAlias) {
			jpql.append(ailas);
		}
		jpql.append(propertyName);
		jpql.append(operator);
		jpql.append("(");
		if (BlankUtils.isBlank(criteria)) {
			jpql.append(StringUtils.join(PLACEHOLDER, values.size(), ","));
		} else {
			criteria.getJpql(jpql);
		}
		jpql.append(")");
		return jpql;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getParameters(java.util.
	 *      Collection)
	 */
	@Override
	public Collection<Object> getParameters(Collection<Object> parameters) {
		if (BlankUtils.isBlank(criteria)) {
			parameters.addAll(values);
		} else {
			criteria.getParameters(parameters);
		}
		return parameters;
	}

}
